package cn.rdq.iknessplatform.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.rdq.api.dto.BlogDetailDto;
import cn.rdq.api.dto.BlogShowDto;
import cn.rdq.api.po.Blog;
import cn.rdq.api.vo.BlogVo;
import cn.rdq.dao.BlogMapper;
import cn.rdq.iknessplatform.service.BlogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Transactional
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Blog> listBlog() {
        return null;
    }

    @Override
    public List<Blog> blogByUserId(Long userId) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return list(wrapper);
    }

    @Override
    public BlogDetailDto getDtoById(Long blogId) {
        return blogMapper.getDetailById(blogId);
    }

    @Override
    public List<Blog> hotBlog() {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.select("id", "title")
                .orderByDesc("create_time");
        PageHelper.startPage(1, 10);
        return list(wrapper);
    }

    @Override
    public List<BlogShowDto> listBlogBrief(BlogVo blogVo) {
        PageHelper.startPage(blogVo.getCurrentPage(), blogVo.getPageSize());
        return blogMapper.listBlogBrief(blogVo);
    }

    @Override
    public void viewsIncrease(Long blogId) {
        Blog blog = getById(blogId);
        if (!ObjectUtils.isEmpty(blog)) {
            blog.setViews(blog.getViews() + 1);
            updateById(blog);
        }
    }

    @Override
    public Blog publishBlog(Blog blog) {
        UpdateWrapper<Blog> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", blog.getId());
        saveOrUpdate(blog);
        return blog;
    }
}
